/**
 * Copyright (c) 2009 Koder Chen. All Rights Reserved.
 *
 * tst_rsa.h
 *
 * The file declares the functions for the test of rsa logic
 *
 *  Author: chenfuhan
 * Created: 2009-6-24
 * Version: 1.0
 */
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>

#include "tst_rsa.h"
#include "tst_helper.h"
#include "prime.h"

void logmsg(char *msg)
{
	printf("%s", msg);
	fflush(stdout);
}

void test_int2bytes()
{
	logmsg("Test int2bytes...");

	srand(time(NULL));
	u8_pt a1 = rsa_default_malloc();
	u8_pt a2 = rsa_default_malloc();
	i32_t t1 = 0, i = 0;

	for(i = 0; i < 10000; i++)
	{
		t1 = rand() % 32767;
		bi_reset(a1);
		bi_reset(a2);
		int2bytes(t1, a1);
		rsa_conv_int2bytes(t1, a2);
		assert(bi_comp(a1, a2) == 0);
	}
	rsa_free(a1);
	rsa_free(a2);

	logmsg("Ok\n");
}

void test_bytes2int()
{
	srand(time(NULL));
	u8_pt a1 = rsa_default_malloc();
	i32_t t1 = 0, t2 = 0, i = 0;

	for(i = 0; i < 10000; i++)
	{
		t1 = rand() % 32767;
		bi_reset(a1);
		int2bytes(t1, a1);
		t2 = rsa_conv_bytes2int(a1);
		assert(t1 == t2);
	}
	rsa_free(a1);
}

void test_rand_bits()
{

}

void test_rand_int()
{
}

void test_fermat_test()
{
	logmsg("Test fermat test...........");

	u8_pt a1 = rsa_default_malloc();
	i32_t i = 0, j = 0;

	while(small_prime_numbers[i] != 0)
	{
		bi_reset(a1);
		int2bytes(small_prime_numbers[i], a1);
		j = rsa_fermat_test(a1);
		assert(j == 1);
		++ i;
	}
	rsa_free(a1);

	logmsg("Ok\n");
}

void test_jacobi()
{
	logmsg("Test jacobi...........");

	srand(time(NULL));
	u8_pt a1 = rsa_default_malloc();
	u8_pt a2 = rsa_default_malloc();
	u32_t t1 = 0, t2 = 0, t3 = 0, t4 = 0;
	i32_t i = 0, j = 0;

	for(i = 0; i < 100; i++)
	{
		t1 = rand() % 32767 + 1;
		bi_reset(a1);
		int2bytes(t1, a1);
		for(j = 0; j < 100;j++)
		{
			t2 = rand() % 32767 + 1;
			bi_reset(a2);
			int2bytes(t2, a2);
			t3 = jacobi(t1, t2);
			t4 = rsa_jacobi(a1, a2);
			assert(t3 == t4);
		}
	}
	rsa_free(a1);
	rsa_free(a2);
	logmsg("Ok\n");
}

void test_jacobi_test()
{
	logmsg("Test jacobi test...........");

	srand(time(NULL));
	u8_pt a1 = rsa_default_malloc();
	i32_t t1 = 0, t2 = 0, t3 = 0, i = 0, j = 0;

	while(small_prime_numbers[i] != 0)
	{
		bi_reset(a1);
		int2bytes(small_prime_numbers[i], a1);
		j = rsa_jacobi_test(a1, 5);
		assert(j == 1);
		++ i;
	}

	for(i = 0; i < 10000; i++)
	{
		t1 = rand() % 32767 + 1;
		bi_reset(a1);
		int2bytes(t1, a1);
		t2 = rsa_jacobi_test(a1, 5);
		t3 = randomize_prime_test(t1, 5);
		if(t2 != t3)
		{
			printf("%d jacobi test: %d %d\n", t1, t2, t3);
			fflush(stdout);
		}
		assert(t2 == t3);
	}
	rsa_free(a1);

	logmsg("Ok\n");
}

void test_rand_prime()
{
}

void test_relatively_prime()
{
}

void test_gen_p_q()
{
	i32_t i = 0;
	u8_pt p = rsa_default_malloc();
	u8_pt q = rsa_default_malloc();
	for(i = 0; i < 10; i++)
	{
		rsa_gen_p_q(32, p, q);

		printf("%d %d\n", bytes2int(p), bytes2int(q));
	}
}

void test_extend_euclid_gcd()
{
}

void test_calculate_keys()
{
}

void test_gen_keys()
{
}

void test_rsa_special()
{
	srand(time(NULL));
	u32_t i = 31618;
	u8_pt a1 = rsa_default_malloc();
	int2bytes(i, a1);
	i = rsa_jacobi_test(a1, 20);
	printf("%d\n", i);
}

void test_rsa()
{
	test_int2bytes();
	test_bytes2int();
	test_rand_bits();
	test_rand_int();
	test_fermat_test();
	test_jacobi();
	test_jacobi_test();
	test_rand_prime();
	test_relatively_prime();
	test_gen_p_q();
	test_extend_euclid_gcd();
	test_calculate_keys();
	test_gen_keys();
	//test_rsa_special();
}




